package com.mobiletrain.org.motherkitch;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.mobiletrain.org.motherkitch.fenlei.FenleiFragment;
import com.mobiletrain.org.motherkitch.goumaiqingdan.GmqdFragment;
import com.mobiletrain.org.motherkitch.shequ.ShequFragment;
import com.mobiletrain.org.motherkitch.shouye.ShowyeFragment;
import com.mobiletrain.org.motherkitch.sliding.CollectActivity;
import com.mobiletrain.org.motherkitch.sliding.CreateActivity;
import com.mobiletrain.org.motherkitch.sliding.MyProductionActivity;
import com.mobiletrain.org.motherkitch.sliding.PhotoActivity;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends ActionBarActivity implements RadioGroup.OnCheckedChangeListener {

    private SlidingMenu mSlidingMenu;
    private RadioGroup mGroup;
    private Toolbar toolbar;
    private boolean isOpen = false;
    private ImageView mImage;
    private TextView mText;
    private ImageView mtoolRight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!checkNetwork()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("网络连接失败").setMessage("请检查网络设置").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent("android.settings.WIRELESS_SETTINGS");
                    startActivity(intent);
                }
            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            }).create().show();
        }


        slidingMenuMth();

        toolBarMth();
    }

    public void slidingMenuMth() {
        //slidingMenu
        mSlidingMenu = new SlidingMenu(this);
        mSlidingMenu.setMode(SlidingMenu.LEFT);
        //设置侧滑布局
        mSlidingMenu.setMenu(R.layout.menu_left);
        //设置在前面的模式
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
        //设置后面的模式
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

        mSlidingMenu.setShadowWidth(50);
        int width = outMetrics.widthPixels * 2 / 3;
        mSlidingMenu.setBehindWidth(width);
        mSlidingMenu.setShadowDrawable(R.drawable.shadow);
        //滑动比例
        mSlidingMenu.setBehindScrollScale(0.4f);
        mSlidingMenu.attachToActivity(MainActivity.this,
                SlidingMenu.TOUCHMODE_MARGIN);

        View menu = mSlidingMenu.getMenu();


        ListView slidingList = (ListView) menu.findViewById(R.id.sliding_list);

        View sliding_head = getLayoutInflater().inflate(R.layout.sliding_head, null);
        TextView username = (TextView) sliding_head.findViewById(R.id.username);


        int[] icons = new int[]{R.drawable.left_love,
                R.drawable.left_mine,
                R.drawable.left_comment
                , R.drawable.left_addfood,
                R.drawable.left_post,
                R.drawable.left_reply,
                R.drawable.left_message,
                R.drawable.left_pregnancy,
                R.drawable.left_set};
        String[] msgs = new String[]{"我的收藏", "我的作品", "我的评论", "创建菜谱",
                "我的帖子", "我的回帖", "消息提示", "妈妈社区", "设置"};
        ArrayList<HashMap<String, Object>> arrayList =
                new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < icons.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("icon", icons[i]);
            map.put("msg", msgs[i]);
            arrayList.add(map);
        }

        SimpleAdapter simpleAdapter = new
                SimpleAdapter(this, arrayList,
                R.layout.sliding_item,
                new String[]{"icon", "msg"},
                new int[]{R.id.slid_icon, R.id.slid_msg});

        slidingList.addHeaderView(sliding_head);
        slidingList.setAdapter(simpleAdapter);
        slidingList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent();
                switch (position) {
                    case 0:
                        i.setClass(MainActivity.this, PhotoActivity.class);
                        break;
                    case 1:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 2:
                        i.setClass(MainActivity.this, MyProductionActivity.class);
                        break;
                    case 3:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 4:
                        i.setClass(MainActivity.this, CreateActivity.class);
                        break;
                    case 5:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 6:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 7:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 8:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                    case 9:
                        i.setClass(MainActivity.this, CollectActivity.class);
                        break;
                }
                startActivity(i);
            }
        });
    }

    public void toolBarMth() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mImage = (ImageView) toolbar.findViewById(R.id.toolbarImage);
        mText = (TextView) toolbar.findViewById(R.id.toolbarTitle);

        mtoolRight = (ImageView) toolbar.findViewById(R.id.toolRight);
        setSupportActionBar(toolbar);
        mGroup = (RadioGroup) findViewById(R.id.group);


        mGroup.setOnCheckedChangeListener(this);
        ((RadioButton) mGroup.getChildAt(0)).setChecked(true);


        mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen) {
                    mSlidingMenu.showContent();
                    isOpen = false;
                } else {
                    mSlidingMenu.showMenu();
                    isOpen = true;
                }
            }
        });


    }


    private boolean checkNetwork() {
        ConnectivityManager conn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo net = conn.getActiveNetworkInfo();
        if (net != null && net.isConnected()) {
            return true;
        }
        return false;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment fragment = null;
        switch (checkedId) {
            case R.id.item1:
                mImage.setVisibility(View.VISIBLE);
                mImage.setImageResource(R.drawable.user);
                mtoolRight.setImageResource(R.drawable.search);
                mtoolRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this, SearchActivity.class);
                        startActivity(i);
                    }
                });
                mText.setText("妈妈厨房");
                fragment = ShowyeFragment.newInstance("", "");

                break;
            case R.id.item2:
                mText.setText("菜谱分类");
                mImage.setVisibility(View.GONE);

                mtoolRight.setImageResource(R.drawable.search);
                mtoolRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this, SearchActivity.class);
                        startActivity(i);
                    }
                });
                fragment = FenleiFragment.newInstance("", "");
                break;
            case R.id.item3:
                mText.setText("美食社区");
                mImage.setVisibility(View.GONE);
                mtoolRight.setImageResource(R.drawable.post);
                mtoolRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this, PosterActivity.class);
                        startActivity(i);

                    }
                });
                fragment = ShequFragment.newInstance("", "");
                break;
            case R.id.item4:
                mImage.setVisibility(View.GONE);
                mtoolRight.setImageResource(R.drawable.post);
                mtoolRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(MainActivity.this, PosterActivity.class);
                        startActivity(i);
                    }
                });
                mText.setText("购买清单");
                fragment = GmqdFragment.newInstance("", "");
                break;
        }
        ft.replace(R.id.fragment_container, fragment);
        ft.commit();

    }

    long currentTime;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - currentTime > 2000) {
            currentTime = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次退出",
                    Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
    }

}
